//
//  mm_GetTelNumberForSendMessageViewController.m
//  HondaPro_2
//
//  Created by Eric on 13-8-22.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "mm_GetTelNumberForSendMessageViewController.h"
#import "MainMenuDataModel.h"

@interface mm_GetTelNumberForSendMessageViewController ()

@end

@implementation mm_GetTelNumberForSendMessageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.frame = CGRectMake(0, 0, 500, 600);
    [MainMenuDataModel sharedMainMenuDataModel].strCurrentTelNumber = [MainMenuDataModel sharedMainMenuDataModel].strDefaultTelNumber;
    self.telNumberTxt.text = [MainMenuDataModel sharedMainMenuDataModel].strCurrentTelNumber;
    
    [self registerNotificationForKeyBoard];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setTelNumberTxt:nil];
    [super viewDidUnload];
}

//保存输入的手机号码
-(void)saveTelNumber
{
    //保存数据
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    if ([HelpClass isTelNumber:self.telNumberTxt.text])
    {
        modelIns.strCurrentTelNumber = self.telNumberTxt.text;
        modelIns.strDefaultTelNumber = modelIns.strCurrentTelNumber;
    }
    
    modelIns.dicJobSheetMsg = [NSMutableDictionary dictionaryWithDictionary:modelIns.dicJobSheetMsg];
    [modelIns.dicJobSheetMsg setObject:modelIns.strCurrentTelNumber forKey:@"sendMessage"];
}

#pragma mark - 键盘托起屏幕的代理方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.superview.frame.size.width, self.view.superview.frame.size.height);
    self.view.superview.frame = rect;
    [UIView commitAnimations];
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.keyboardType = UIKeyboardTypeNumberPad;
    
    int offset = 150;//键盘高度350
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.view.superview.frame.size.width;
    float height = self.view.superview.frame.size.height;
    if(offset > 0)
    {
        CGRect rect = CGRectMake(0.0f, -offset,width,height);
        self.view.superview.frame = rect;
    }
    [UIView commitAnimations];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs;
    cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789\n"]invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
    BOOL canChange = [string isEqualToString:filtered];
    
    //只能输入带小数点后两位，判断如果有小数点
    //    if ([textField.text rangeOfString:@"."].length == 1
    //        &&
    //        [textField.text length]-([textField.text rangeOfString:@"."].location+1)>=2
    //        )
    //    {
    //        canChange = NO;
    //    }
    
//    canChange = ![HelpClass isTelNumber:self.telNumberTxt.text];
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:@""];
    if ([HelpClass isTelNumber:toBeString])
    {
        canChange = NO;
    }
    
    return canChange;
}

//注册键盘消息
-(void)registerNotificationForKeyBoard
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setViewNormal) name:UIKeyboardWillHideNotification object:nil];
}

//视图回归正常状态
-(void)setViewNormal
{
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.superview.frame.size.width, self.view.superview.frame.size.height);
    self.view.superview.frame = rect;
    [UIView commitAnimations];
}

@end
